package br.com.fiap.mercadinho.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.fiap.mercadinho.bean.Stock;
import br.com.fiap.mercadinho.common.DAOException;
import br.com.fiap.mercadinho.common.RequestKey;
import br.com.fiap.mercadinho.dao.StockDAO;

/**
 * Servlet implementation class StockServlet
 */
@WebServlet("/stock")
public class StockServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StockServlet() {
		super();
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		

		StockDAO dao = new StockDAO();

	try {
		List<Stock> stockProducts = dao.findAllStockProducts();
		request.setAttribute("stockList", stockProducts);
		sendResponse(request, response);

	} catch (DAOException e) {
		request.setAttribute(RequestKey.INFO_MESSAGE_KEY.getValue(),
				e.getMessage());
		sendResponse(request, response);
	}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		StockDAO dao = new StockDAO();

		try {
			String action = request.getParameter("action");
			request.setAttribute("action", action);
			// refresh stock products list
			request.setAttribute("stockList", dao.findAllStockProducts());

			if (action.equals("show")) {
				List<Stock> stockProducts = dao.findAllStockProducts();
				request.setAttribute("stockList", stockProducts);
			}/*
			 * else if (action.equals("add")) { Stock stock =
			 * getStockProductFromRequest(request); if
			 * (!isValidStockProduct(request, stock)) { sendResponse(request,
			 * response); return; } else { dao.sa List<Product> products =
			 * dao.findAllProducts(); request.setAttribute("productList",
			 * products); } }
			 */else if (action.equals("select")) {
				Integer idStock = Integer.parseInt(request
						.getParameter("stockIdSelected"));
				Stock stock = dao.findStockProductById(idStock);
				request.setAttribute("stock", stock);
				List<Stock> stockProducts = dao.findAllStockProducts();
				request.setAttribute("stockList", stockProducts);
			} else if (action.equals("update")) {
				Stock stock = getStockProductFromRequest(request);
				if (!isValidStockProduct(request, stock)) {
					sendResponse(request, response);
					return;
				} else {
					dao.updateProductStock(stock);
					List<Stock> stockProducts = dao.findAllStockProducts();
					request.setAttribute("stockList", stockProducts);
					request.setAttribute("action", "show");

				}
			}
			sendResponse(request, response);
		} catch (DAOException e) {
			request.setAttribute(RequestKey.INFO_MESSAGE_KEY.getValue(),
					e.getMessage());
			sendResponse(request, response);
		}

	}

	private void sendResponse(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("stock.jsp").forward(request, response);
	}

	private Stock getStockProductFromRequest(HttpServletRequest request) {
		Stock stock = new Stock();
		stock.setId(request.getParameter("stockIdSelected"));
		stock.setQuantity(request.getParameter("quantity"));
		stock.setProductId(request.getParameter("productId"));

		return stock;
	}

	private boolean isValidStockProduct(HttpServletRequest request, Stock stock) {

		String validateResult = stock.validateStock();

		if (!validateResult.equals("")) {
			request.setAttribute(RequestKey.INFO_MESSAGE_KEY.getValue(),
					validateResult);
			request.setAttribute("stock", getStockProductFromRequest(request));
			return false;
		}
		return true;

	}

}
